<?php
/**
 * Serve up html for a post
 */
$golf_course_id = (int) get_input('golf_course_id');
$score_type = get_input('score_type');
$tee = get_input('tee');
if(!$golf_course_id) {
	$golf_course_id = elgg_extract('golf_course_id', $vars, '');
	$score_type = elgg_extract('score_type', $vars, '');
	$tee = elgg_extract('tee', $vars, '');
	$score = elgg_extract('score', $vars, '');
} else {
	$golf_score_id = get_input('golf_score_id');
	if($golf_score_id && $golf_score_id > 0) {
		$score = elgg_get_golf_score(array('golf_score_id' => $golf_score_id));
		$score = $score[0];
	}
}
if($score_type) {
	$score_type = (int)$score_type;
}
$golf_course = elgg_get_golf_course(array('golf_course_id' => $golf_course_id));
$golf_course = $golf_course[0];

if($score_type == 1) {
	$golf_course_cards = elgg_get_golf_course_card(array('golf_course_id' => $golf_course_id));
	$distance_map = array();
	$par_map = array();
	$distance_total = 0;
	$distance_in = 0;
	$distance_out = 0;
	$par_total = 0;
	$par_in = 0;
	$par_out = 0;
	foreach ($golf_course_cards as $golf_course_card) {
		$_distance = '';
		if($tee == 'gold') {
			$_distance = $golf_course_card->gold_tee_distance;
		} else if($tee == 'blue') {
			$_distance = $golf_course_card->blue_tee_distance;
		} else if($tee == 'white') {
			$_distance = $golf_course_card->white_tee_distance;
		} else if($tee == 'red') {
			$_distance = $golf_course_card->red_tee_distance;
		}
		$distance_map['' . $golf_course_card->hole] = $_distance;
		$par_map['' . $golf_course_card->hole] = $golf_course_card->par;
		$distance_total += $_distance;
		$par_total += $golf_course_card->par;
		if($golf_course_card->hole < 10) {
			$distance_out += $_distance;
			$par_out += $golf_course_card->par;
		} else {
			$distance_in += $_distance;
			$par_in += $golf_course_card->par;
		}
	}
} else {
	$distance_total = 0;
	if($tee == 'gold') {
		$distance_total = $golf_course->gold_total;
	} else if($tee == 'blue') {
		$distance_total = $golf_course->blue_total;
	} else if($tee == 'white') {
		$distance_total = $golf_course->white_total;
	} else if($tee == 'red') {
		$distance_total = $golf_course->red_total;
	}
	$par_total = $golf_course->par_total;
}

$rating = 0;
$slope = 0;
if($tee == 'gold') {
	$rating = $golf_course->gold_course_rating;
	$slope = $golf_course->gold_course_slope;
} else if($tee == 'blue') {
	$rating = $golf_course->blue_course_rating;
	$slope = $golf_course->blue_course_slope;
} else if($tee == 'white') {
	$rating = $golf_course->white_course_rating;
	$slope = $golf_course->white_course_slope;
} else if($tee == 'red') {
	$rating = $golf_course->red_course_rating;
	$slope = $golf_course->red_course_slope;
}

if($score) {
	if($score_type == 1) {
		$score_details = elgg_get_golf_score_detail(array('golf_score_id' => $score->golf_score_id));
		$score_map = array();
		$score_total = 0;
		$score_in = 0;
		$score_out = 0;
		foreach ($score_details as $score_detail) {
			$score_map['' . $score_detail->hole] = $score_detail->score;
			$score_total += $score_detail->score;
			if($score_detail->hole <= 9) {
				$score_out += $score_detail->score;
			} else {
				$score_in += $score_detail->score;
			}
		}
	} else {
		$score_total = $score->total_score;
	}
}

?>
<div id="__tee_color" class="elgg-golfscore-icon-tee-<?php echo getTeeName($tee, $golf_course); ?>">&nbsp;</div>
<label>
	<?php echo elgg_echo('golfscore:add:tee'); ?>
</label>
<?php echo elgg_view('input/dropdown', array(
	'class' => 'elgg-golfscore-select-tee', 
	'name' => 'tee', 
	'onchange' => 'onSelectCourse();',
	'id' => '__tee',
	'value' => $tee,
	'options_values' => array(
		'gold' => elgg_echo('golfscore:tee:' . $golf_course->gold_tee_name),
		'blue' => elgg_echo('golfscore:tee:' . $golf_course->blue_tee_name),
		'white' => elgg_echo('golfscore:tee:' . $golf_course->white_tee_name),
		'red' => elgg_echo('golfscore:tee:' . $golf_course->red_tee_name),
	),
)); ?>
<label>
	<?php echo elgg_echo('golfscore:add:rating'); ?>
</label>
<?php echo elgg_view('input/text', array(
	'class' => 'elgg-golfscore-text-rating', 
	'name' => 'rating',
	'id' => '__rating',
	'value' => $rating,
)); ?>
<label>
	<?php echo elgg_echo('golfscore:add:slope'); ?>
</label>
<?php echo elgg_view('input/text', array(
	'class' => 'elgg-golfscore-text-slope', 
	'name' => 'slope', 
	'id' => '__slope',
	'value' => $slope,
)); ?>
<div style="margin-top: 10px;">
	<div class="rowHoleBox" style="border-top:1px solid #999999;background-color:#eeeeee;">
		<div class="totalHoleBox"><?php echo elgg_echo('golfscore:add:total'); ?></div>
		<?php if($score_type == 1) {?>
		<div class="totalHoleBox"><?php echo elgg_echo('golfscore:add:in'); ?></div>
		<div class="editHoleBox">18</div>
		<div class="editHoleBox">17</div>
		<div class="editHoleBox">16</div>
		<div class="editHoleBox">15</div>
		<div class="editHoleBox">14</div>
		<div class="editHoleBox">13</div>
		<div class="editHoleBox">12</div>
		<div class="editHoleBox">11</div>
		<div class="editHoleBox">10</div>
		<div class="totalHoleBox"><?php echo elgg_echo('golfscore:add:out'); ?></div>
		<div class="editHoleBox">9</div>
		<div class="editHoleBox">8</div>
		<div class="editHoleBox">7</div>
		<div class="editHoleBox">6</div>
		<div class="editHoleBox">5</div>
		<div class="editHoleBox">4</div>
		<div class="editHoleBox">3</div>
		<div class="editHoleBox">2</div>
		<div class="editHoleBox">1</div>
		<?php }?>
		<div class="labelHoleBox"><?php echo elgg_echo('golfscore:add:hole'); ?></div>
	</div>
	<div class="rowHoleBox" style="background-color:#ffffff;" id="__distance">
		<div id="__distance_total" class="totalHoleBox"><?php echo $distance_total; ?></div>
		<?php if($score_type == 1) {?>
		<div id="__distance_out" class="totalHoleBox"><?php echo $distance_out; ?></div>
		<div id="__distance_18" class="editHoleBox"><?php echo $distance_map[18]; ?></div>
		<div id="__distance_17" class="editHoleBox"><?php echo $distance_map[17]; ?></div>
		<div id="__distance_16" class="editHoleBox"><?php echo $distance_map[16]; ?></div>
		<div id="__distance_15" class="editHoleBox"><?php echo $distance_map[15]; ?></div>
		<div id="__distance_14" class="editHoleBox"><?php echo $distance_map[14]; ?></div>
		<div id="__distance_13" class="editHoleBox"><?php echo $distance_map[13]; ?></div>
		<div id="__distance_12" class="editHoleBox"><?php echo $distance_map[12]; ?></div>
		<div id="__distance_11" class="editHoleBox"><?php echo $distance_map[11]; ?></div>
		<div id="__distance_10" class="editHoleBox"><?php echo $distance_map[10]; ?></div>
		<div id="__distance_out" class="totalHoleBox"><?php echo $distance_in; ?></div>
		<div id="__distance_9" class="editHoleBox"><?php echo $distance_map[9]; ?></div>
		<div id="__distance_8" class="editHoleBox"><?php echo $distance_map[8]; ?></div>
		<div id="__distance_7" class="editHoleBox"><?php echo $distance_map[7]; ?></div>
		<div id="__distance_6" class="editHoleBox"><?php echo $distance_map[6]; ?></div>
		<div id="__distance_5" class="editHoleBox"><?php echo $distance_map[5]; ?></div>
		<div id="__distance_4" class="editHoleBox"><?php echo $distance_map[4]; ?></div>
		<div id="__distance_3" class="editHoleBox"><?php echo $distance_map[3]; ?></div>
		<div id="__distance_2" class="editHoleBox"><?php echo $distance_map[2]; ?></div>
		<div id="__distance_1" class="editHoleBox"><?php echo $distance_map[1]; ?></div>
		<?php }?>
		<div class="labelHoleBox"><?php echo elgg_echo('golfscore:add:distance'); ?></div>
	</div>
	<div class="rowHoleBox" style="background-color:#eeeeee;" id="__par">
		<div id="__par_total" class="totalHoleBox"><?php echo $par_total; ?></div>
		<?php if($score_type == 1) {?>
		<div id="__par_in" class="totalHoleBox"><?php echo $par_in; ?></div>
		<div id="__par_18" class="editHoleBox"><?php echo $par_map[18]; ?></div>
		<div id="__par_17" class="editHoleBox"><?php echo $par_map[17]; ?></div>
		<div id="__par_16" class="editHoleBox"><?php echo $par_map[16]; ?></div>
		<div id="__par_15" class="editHoleBox"><?php echo $par_map[15]; ?></div>
		<div id="__par_14" class="editHoleBox"><?php echo $par_map[14]; ?></div>
		<div id="__par_13" class="editHoleBox"><?php echo $par_map[13]; ?></div>
		<div id="__par_12" class="editHoleBox"><?php echo $par_map[12]; ?></div>
		<div id="__par_11" class="editHoleBox"><?php echo $par_map[11]; ?></div>
		<div id="__par_10" class="editHoleBox"><?php echo $par_map[10]; ?></div>
		<div id="__par_out" class="totalHoleBox"><?php echo $par_out; ?></div>
		<div id="__par_9" class="editHoleBox"><?php echo $par_map[9]; ?></div>
		<div id="__par_8" class="editHoleBox"><?php echo $par_map[8]; ?></div>
		<div id="__par_7" class="editHoleBox"><?php echo $par_map[7]; ?></div>
		<div id="__par_6" class="editHoleBox"><?php echo $par_map[6]; ?></div>
		<div id="__par_5" class="editHoleBox"><?php echo $par_map[5]; ?></div>
		<div id="__par_4" class="editHoleBox"><?php echo $par_map[4]; ?></div>
		<div id="__par_3" class="editHoleBox"><?php echo $par_map[3]; ?></div>
		<div id="__par_2" class="editHoleBox"><?php echo $par_map[2]; ?></div>
		<div id="__par_1" class="editHoleBox"><?php echo $par_map[1]; ?></div>
		<?php }?>
		<div class="labelHoleBox"><?php echo elgg_echo('golfscore:add:par'); ?></div>
	</div>
	<div class="rowHoleBox" style="background-color:#ffffff;">
		<?php if($score_type == 1) {?>
		<div id="__score_total" class="totalHoleBox"><?php echo $score_total;?></div>
		<div id="__score_in" class="totalHoleBox"><?php echo $score_in;?></div>
		<div id="__score_18" class="editHoleBox">
			<input id="__score_18_input" name="score_18" value="<?php echo $score_map[18];?>" type="text" class="editHoleText" onblur="onFinishInputScore(18);" value="<?php echo $par_map[18]; ?>">
		</div>
		<div id="__score_17" class="editHoleBox">
			<input id="__score_17_input" name="score_17" value="<?php echo $score_map[17];?>" type="text" class="editHoleText" onblur="onFinishInputScore(17);" value="<?php echo $par_map[17]; ?>">
		</div>
		<div id="__score_16" class="editHoleBox">
			<input id="__score_16_input" name="score_16" value="<?php echo $score_map[16];?>" type="text" class="editHoleText" onblur="onFinishInputScore(16);" value="<?php echo $par_map[16]; ?>">
		</div>
		<div id="__score_15" class="editHoleBox">
			<input id="__score_15_input" name="score_15" value="<?php echo $score_map[15];?>" type="text" class="editHoleText" onblur="onFinishInputScore(15);" value="<?php echo $par_map[15]; ?>">
		</div>
		<div id="__score_14" class="editHoleBox">
			<input id="__score_14_input" name="score_14" value="<?php echo $score_map[14];?>" type="text" class="editHoleText" onblur="onFinishInputScore(14);" value="<?php echo $par_map[14]; ?>">
		</div>
		<div id="__score_13" class="editHoleBox">
			<input id="__score_13_input" name="score_13" value="<?php echo $score_map[13];?>" type="text" class="editHoleText" onblur="onFinishInputScore(13);" value="<?php echo $par_map[13]; ?>">
		</div>
		<div id="__score_12" class="editHoleBox">
			<input id="__score_12_input" name="score_12" value="<?php echo $score_map[12];?>" type="text" class="editHoleText" onblur="onFinishInputScore(12);" value="<?php echo $par_map[12]; ?>">
		</div>
		<div id="__score_11" class="editHoleBox">
			<input id="__score_11_input" name="score_11" value="<?php echo $score_map[11];?>" type="text" class="editHoleText" onblur="onFinishInputScore(11);" value="<?php echo $par_map[11]; ?>">
		</div>
		<div id="__score_10" class="editHoleBox">
			<input id="__score_10_input" name="score_10" value="<?php echo $score_map[10];?>" type="text" class="editHoleText" onblur="onFinishInputScore(10);" value="<?php echo $par_map[10]; ?>">
		</div>
		<div id="__score_out" class="totalHoleBox"><?php echo $score_out;?></div>
		<div id="__score_9" class="editHoleBox">
			<input id="__score_9_input" name="score_9"  value="<?php echo $score_map[9];?>"type="text" class="editHoleText" onblur="onFinishInputScore(9);" value="<?php echo $par_map[9]; ?>">
		</div>
		<div id="__score_8" class="editHoleBox">
			<input id="__score_8_input" name="score_8" value="<?php echo $score_map[8];?>" type="text" class="editHoleText" onblur="onFinishInputScore(8);" value="<?php echo $par_map[8]; ?>">
		</div>
		<div id="__score_7" class="editHoleBox">
			<input id="__score_7_input" name="score_7" value="<?php echo $score_map[7];?>" type="text" class="editHoleText" onblur="onFinishInputScore(7);" value="<?php echo $par_map[7]; ?>">
		</div>
		<div id="__score_6" class="editHoleBox">
			<input id="__score_6_input" name="score_6" value="<?php echo $score_map[6];?>" type="text" class="editHoleText" onblur="onFinishInputScore(6);" value="<?php echo $par_map[6]; ?>">
		</div>
		<div id="__score_5" class="editHoleBox">
			<input id="__score_5_input" name="score_5" value="<?php echo $score_map[5];?>" type="text" class="editHoleText" onblur="onFinishInputScore(5);" value="<?php echo $par_map[5]; ?>">
		</div>
		<div id="__score_4" class="editHoleBox">
			<input id="__score_4_input" name="score_4" value="<?php echo $score_map[4];?>" type="text" class="editHoleText" onblur="onFinishInputScore(4);" value="<?php echo $par_map[4]; ?>">
		</div>
		<div id="__score_3" class="editHoleBox">
			<input id="__score_3_input" name="score_3" value="<?php echo $score_map[3];?>" type="text" class="editHoleText" onblur="onFinishInputScore(3);" value="<?php echo $par_map[3]; ?>">
		</div>
		<div id="__score_2" class="editHoleBox">
			<input id="__score_2_input" name="score_2" value="<?php echo $score_map[2];?>" type="text" class="editHoleText" onblur="onFinishInputScore(2);" value="<?php echo $par_map[2]; ?>">
		</div>
		<div id="__score_1" class="editHoleBox" style="">
			<input id="__score_1_input" name="score_1" value="<?php echo $score_map[1];?>" type="text" class="editHoleText" onblur="onFinishInputScore(1);" value="<?php echo $par_map[1]; ?>">
		</div>
		<?php } else {?>
		<div id="__score_total" class="totalHoleBox">
			<input id="__score_total_input" name="score_total" value="<?php echo $score_total;?>" type="text" class="editHoleText">
		</div>
		<?php }?>
		<div class="labelHoleBox"><?php echo elgg_echo('golfscore:add:score'); ?></div>
	</div>
</div>